package com.bjpowernode.service.impl;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.manager.CustomerManager;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.model.TCustomer;
import com.bjpowernode.query.CustomerQuery;
import com.bjpowernode.result.CustomerExcelData;
import com.bjpowernode.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CustomerServiceImpl  implements CustomerService {
    @Resource
    CustomerManager customerManager;
    @Resource
    TCustomerMapper tCustomerMapper;
    @Override
    public Boolean convertCustomer(CustomerQuery customerQuery) {
        return customerManager.convertCustomer(customerQuery);
    }

    @Override
    public PageInfo<TCustomer> getCustomerByPage(Integer current) {
        //1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //2.查询
        List<TCustomer> list = tCustomerMapper.selectCustomerPage();
        //3.封装分页数据到PageInfo
        PageInfo<TCustomer> info = new PageInfo<>(list);

        return info;
    }

    @Override
    public void exportExcel(ServletOutputStream outputStream,String ids) {
               List<String> list=new ArrayList<>();
             if (StringUtils.hasText(ids)){
                 list = Arrays.asList(ids.split(","));
             }
        List<TCustomer> tCustomerList = tCustomerMapper.selectCustomerByExcel(list);
        List<CustomerExcelData> customerExcelDataList = new ArrayList<>();
        tCustomerList.forEach(tCustomer -> {
            CustomerExcelData customerExcelData = new CustomerExcelData();

            //此处无法直接复制，因为 CustomerExcelData这个Excel模版类和 TCustomer类相差很大，比如: 年收入字段，
            // CustomerExcelData这个类直接有一个年收入字段， 而TCustomer类在关联的TClueDO类里面才用这个字段，所以无法直接复制
            customerExcelData.setOwnerName(ObjectUtils.isEmpty(tCustomer.getOwnerDO()) ? Constants.EMPTY : tCustomer.getOwnerDO().getName());
            customerExcelData.setActivityName(ObjectUtils.isEmpty(tCustomer.getActivityDO()) ? Constants.EMPTY : tCustomer.getActivityDO().getName());
            customerExcelData.setFullName(tCustomer.getClueDO().getFullName());
            customerExcelData.setAppellationName(tCustomer.getAppellationDO().getTypeValue());
            customerExcelData.setPhone(tCustomer.getClueDO().getPhone());
            customerExcelData.setWeixin(tCustomer.getClueDO().getWeixin());
            customerExcelData.setQq(tCustomer.getClueDO().getQq());
            customerExcelData.setEmail(tCustomer.getClueDO().getEmail());
            customerExcelData.setAge(tCustomer.getClueDO().getAge());
            customerExcelData.setJob(tCustomer.getClueDO().getJob());
            customerExcelData.setYearIncome(tCustomer.getClueDO().getYearIncome());
            customerExcelData.setAddress(tCustomer.getClueDO().getAddress());
            customerExcelData.setNeedLoadName(tCustomer.getNeedLoanDO().getTypeValue());
            customerExcelData.setProductName(tCustomer.getIntentionProductDO().getName());
            customerExcelData.setSourceName(tCustomer.getSourceDO().getTypeValue());
            customerExcelData.setDescription(tCustomer.getDescription());
            customerExcelData.setNextContactTime(tCustomer.getNextContactTime());

            customerExcelDataList.add(customerExcelData);
        });

        //write()方法第一个参数是 目的地（Excel文件要输出到哪里去），第二个参数是生成Excel的模板类
        EasyExcel.write(outputStream, CustomerExcelData.class)
                .sheet()
                .doWrite(customerExcelDataList);
    }

    @Override
    public TCustomer getCustomerById(Integer id) {
        return tCustomerMapper.selectByPrimaryKey(id);
    }
}
